<template>
  <div class="divBox">
      <el-upload class="upload-demo" style="width:300px" ref="upload" :show-file-list="false" action :auto-upload="false" :on-change="uploadFile" :on-remove="deleteFile">
        <el-button size="medium" type="primary" icon="el-icon-upload" :loading="progress!=0&&progress!=100" >点击上传</el-button>
        <a-progress type="circle" style="margin: 5px 2px" :text-inside="true" :width="28" :percent="progress" :hidden="progress==0"/>
      </el-upload>
  </div>
</template>
<script>
  import {client} from '@/api/oss'
  const AppInfoParser = require('app-info-parser')

  export default {
    props: {
      value: {
        type: [String, Array,Object],
        required: false,
      }
    },
    data() {
      return {
        progress: 0,
        uploadId:'',
      }
    },
    methods: {
      deleteFile(files){
        this.progress=0
        //console.error(files)
      },
      async uploadFile(files) {
        let that = this;
        that.progress=1
        let file = files.raw;

       // console.error(files)
       // that.$message.success("https://files.gamepro.vip/upload/"+file.size);
        let parallel= 100;
        let partSize =100 * 1024;
        let fileSize=(file.size/ 1024 / 1024).toFixed(2)
      //  console.error(file.size)
        if (fileSize >=100){
          partSize=512*1024
        }
        const options = {
          // 获取分片上传进度、断点和返回值。
          progress: (p, cpt) => {
            that.uploadId = cpt.uploadId;//取消时需要的参数
            that.progress = p<=0.01?1:Number((p * 100).toFixed(0));//进度条
          },
          // 设置并发上传的分片数量。
          parallel: parallel,
          // 设置分片大小。默认值为1 MB，最小值为100 KB。
          partSize: partSize,
          timeout: 600000,//设置超时时间
        };
        //开始分片上传。
        async function multipartUpload() {
          try {
            //object-name可以自定义为文件名（例如file.txt）或目录（例如abc/test/file.txt）的形式，实现将文件上传至当前Bucket或Bucket下的指定目录。
            let result = await client.multipartUpload("upload/"+file.name, file, {...options,});
            //that.$message.success("https://files.gamepro.vip/upload/"+file.name);
            let fileSizeUnit='MB';
            if (fileSize >=1024){
              fileSize=(fileSize / 1024).toFixed(2)
              fileSizeUnit='GB';
            }
            that.$emit("change","https://loansmanage.oss-cn-beijing.aliyuncs.com/upload/"+file.name);
          } catch (e) {console.log(e);}
        }
        multipartUpload(); //调用函数
      }
    },model: {
      prop: "value",
      event: "change",
    },
  }
</script>
